package com.cn.struts2;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.xmpp.XmppHeaders;

import com.opensymphony.xwork2.Action;

public class LotteryAction extends BaseAction {

	/**
	 * 序列化
	 */
	private static final long serialVersionUID = -5964733689349522935L;

	/**
	 * 测试函数
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		LotteryAction action = new LotteryAction();
		action.buildCommands();
	}

	/**
	 * 日志
	 */
	private Logger logger = Logger.getLogger(LotteryAction.class);
	/**
	 * 开奖号码
	 */
	public String number;
	/**
	 * 倍率
	 */
	public String times;
	/**
	 * 期号
	 */
	public String term;

	/**
	 * 上一期期号
	 */
	public String prev;

	/**
	 * 消息发送通道
	 */
	private MessageChannel xmppOutputChannel;

	/**
	 * 命令文件
	 */
	private String commandsFile;

	private BufferedReader reader;

	/**
	 * 构造函数
	 */
	public LotteryAction() {
		this.prev = "0";
	}

	/**
	 * 构造命令：读取command文件获取命令
	 * 
	 * @return 指令列表
	 * @throws IOException
	 */
	public ArrayList<String> buildCommands() throws IOException {
		String commandfilepath = this.getCommandsFile();
		ArrayList<String> commands = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(commandfilepath));

		String line = null;
		while ((line = reader.readLine()) != null) {
			commands.add(line.trim());
		}
		logger.debug(commands.toString());
		return commands;
	}

	public String getCommandsFile() {
		return commandsFile;
	}

	public String getNumber() {
		return number;
	}

	public BufferedReader getReader() {
		return reader;
	}

	public String getTerm() {
		return term;
	}

	public String getTimes() {
		return times;
	}

	public String getPrev() {
		return prev;
	}

	public void setPrev(String prev) {
		this.prev = prev;
	}

	public MessageChannel getXmppOutputChannel() {
		return xmppOutputChannel;
	}

	/**
	 * 
	 * @return success字符串
	 * @throws IOException
	 */
	public String handler() throws IOException {
		/**
		 * 命令处理
		 */
		String parameters = "number=" + number + ";times=" + times + ";term="
				+ term;

		/**
		 * 重复发送指令处理
		 */
		if (this.prev.equals(this.term)) {
			logger.debug("重复发送");
			return Action.SUCCESS;
		} else {

			this.prev = this.term;
		}

		ArrayList<String> commands = this.buildCommands();
		for (int i = 0; i < commands.size(); ++i) {
			final long startTime = new Date().getTime();
			final String command = commands.get(i).replaceAll("%parameters%",
					parameters);
			String[] iterms = commands.get(i).split(";");
			final String jid = iterms[0];
			long delay = Long.parseLong(iterms[iterms.length - 1]);
			final String playload = command.replace(jid + ";", "").replace(
					";" + iterms[iterms.length - 1], "");

			/**
			 * 延时发送
			 */
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					Message<String> message = MessageBuilder
							.withPayload(playload)
							.setHeader(XmppHeaders.TO, jid).build();
					xmppOutputChannel.send(message);
					this.cancel();
					long endTime = new Date().getTime();
					logger.debug("命令：" + playload + "；延时："
							+ (endTime - startTime) + "毫秒");
				}
			}, delay);

		}
		return Action.SUCCESS;
	}

	public void setCommandsFile(String commandsFile) {
		this.commandsFile = commandsFile;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public void setReader(BufferedReader reader) {
		this.reader = reader;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public void setTimes(String times) {
		this.times = times;
	}

	public void setXmppOutputChannel(MessageChannel xmppOutputChannel) {
		this.xmppOutputChannel = xmppOutputChannel;
	}
}
